package com.board.consumer;

import com.alipay.api.AlipayApiException;
import com.board.dao.SeatEatOrderMapper;
import com.board.dto.PayInfoDto;
import com.board.entity.SeatEatOrder;
import com.board.util.AlipayUtil;
import com.board.util.StateDate;
import org.aspectj.weaver.ast.Var;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "seat_order_queue")
public class SeatEatOrderCon {
    @Autowired
    private AlipayUtil alipayUtil;
    @Autowired
    private SeatEatOrderMapper eatOrderMapper;

    @RabbitHandler
    public void handlerSeatOrder(String orderNum) throws AlipayApiException {
        //调用支付宝的接口查询支付的信息
        PayInfoDto payState = alipayUtil.getPayState(orderNum);
        SeatEatOrder eatOrder = eatOrderMapper.selectById(orderNum);
        if (payState.getPayState().equals("TRADE_SUCCESS")){
            //用户已支付：判断数据库中状态是否修改
            if (eatOrder.getOrderState()== StateDate.SEAT_ORDER_STATE_NOPAY){
                eatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_PAY);
                eatOrder.setPayTime(payState.getPayDate());
                eatOrderMapper.updateById(eatOrder);
            }
        }else{
            //用户未支付:取消订单
            eatOrder.setOrderState(StateDate.SEAT_ORDER_STATE_USER_CANCEL);
            eatOrderMapper.updateById(eatOrder);
        }
    }

}
